package com.kelaite.procurement.utils;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**

 * @author yuanchangwei
 * 
 */

public class JsonUtil
{

	private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);

	private static ObjectMapper objectMapper = new ObjectMapper();
	static
	{
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);// 遇到未定义的属性，不抛异常
	}

	/**
	 * 将对象序列化为JSON字符串
	 * 
	 * @param object
	 * @return JSON字符串
	 */
	public static String serialize(Object object)
	{
		Writer write = new StringWriter();
		try
		{
			objectMapper.writeValue(write, object);
		} catch (JsonGenerationException e)
		{
			logger.error("JsonGenerationException when serialize object to json", e);
		} catch (JsonMappingException e)
		{
			logger.error("JsonMappingException when serialize object to json", e);
		} catch (IOException e)
		{
			logger.error("IOException when serialize object to json", e);
		}
		return write.toString();
	}

	/**
	 * 将JSON字符串反序列化为对象
	 * 
	 * @param object
	 * @return JSON字符串
	 */
	@SuppressWarnings("unchecked")
	public static <T> T deserialize(String json, Class<T> clazz)
	{
		Object object = null;
		try
		{
			object = objectMapper.readValue(json, TypeFactory.rawClass(clazz));
		} catch (JsonParseException e)
		{
			logger.error("JsonParseException when serialize object to json", e);
		} catch (JsonMappingException e)
		{
			logger.error("JsonMappingException when serialize object to json", e);
		} catch (IOException e)
		{
			logger.error("IOException when serialize object to json", e);
		}
		return (T) object;
	}

	/**
	 * 将JSON字符串反序列化为对象
	 * 
	 * @param object
	 * @return JSON字符串
	 */
	@SuppressWarnings("unchecked")
	public static <T> T deserialize(String json, TypeReference<T> typeRef)
	{
		Object obj = null;
		try
		{
			obj = objectMapper.readValue(json, typeRef);
		} catch (JsonParseException e)
		{
			logger.error("JsonParseException when deserialize json", e);
		} catch (JsonMappingException e)
		{
			logger.error("JsonMappingException when deserialize json", e);
		} catch (IOException e)
		{
			logger.error("IOException when deserialize json", e);
		}
		return (T) obj;
	}

	/**
	 * 
	 * 方法用途: 将json串解析为map<br>
	 * 实现步骤: <br>
	 * 
	 * @param contentJson
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> parseJsonToMap(String contentJson)
	{
		if (StringUtils.isBlank(contentJson))
		{
			logger.error("The json string to be parsed to Map is null");
			throw new RuntimeException("The json string to be parsed to Map is null");
		}
		try
		{
			return objectMapper.readValue(contentJson, Map.class);
		} catch (Exception e)
		{
			logger.error("Parse json string to map error", e);
			throw new RuntimeException("Parse json string to map error", e);
		}

	}

	public static String converMapToJson(Map<String, String> map)
	{
		if (null == map || map.isEmpty())
		{
			logger.error("The map to be converted to json is null");
		}
		try
		{
			return objectMapper.writeValueAsString(map);
		} catch (JsonProcessingException e)
		{
			logger.error("Convert map to json error,the map:" + map.values());
			throw new RuntimeException("Convert map to json error,the map:" + map.values(), e);
		}
	}

}
